package io.xccit.train.member.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.RandomUtil;
import io.xccit.train.common.enums.BusinessCode;
import io.xccit.train.common.exception.BusinessException;
import io.xccit.train.common.util.JwtUtil;
import io.xccit.train.common.util.SnowFlakeUtil;
import io.xccit.train.member.mapper.MemberMapper;
import io.xccit.train.member.service.MemberService;
import io.xccit.train.model.member.domain.Member;
import io.xccit.train.model.member.domain.MemberExample;
import io.xccit.train.model.member.domain.dto.MemberDTO;
import io.xccit.train.model.member.domain.dto.MemberLoginDTO;
import io.xccit.train.model.member.domain.vo.MemberVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author CH_ywx
 * @version 1.0
 * @create 2024/11/4
 * <p>会员业务层</p>
 */
@Service
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private JwtUtil jwtUtil;

    /**
     * 查询会员总数
     * @return 会员总数
     */
    @Override
    public Long count() {
        return memberMapper.countByExample(null);
    }

    /**
     * 会员注册
     *
     * @param memberDTO 请求参数
     * @return 会员主键
     */
    @Override
    public Long registry(MemberDTO memberDTO) {
        String mobile = memberDTO.getMobile();
        Member dbMember = getMemberByMobile(mobile);
        if (null != dbMember){
            throw new BusinessException(BusinessCode.MEMBER_MOBILE_EXIST);
        }
        Member member = new Member();
        member.setId(SnowFlakeUtil.nextId());
        member.setMobile(mobile);
        memberMapper.insert(member);
        return member.getId();
    }

    /**
     * 发送验证码
     *
     * @param memberDTO
     * @return 请求结果
     */
    @Override
    public void sendCode(MemberDTO memberDTO) {
        String mobile = memberDTO.getMobile();
        Member dbMember = getMemberByMobile(mobile);
        if (null == dbMember){
            Member member = new Member();
            member.setId(SnowFlakeUtil.nextId());
            member.setMobile(mobile);
            memberMapper.insert(member);
        }
        //TODO 请求阿里云短信接口,这里使用随机数模拟
        int[] ints = RandomUtil.randomInts(6);
        StringBuffer code = new StringBuffer();
        for (int i : ints) {
            code.append(i);
        }
    }

    /**
     * 会员登录
     *
     * @param memberLoginDTO 请求参数
     * @return 会员token
     */
    @Override
    public MemberVO login(MemberLoginDTO memberLoginDTO) {
        String code = memberLoginDTO.getCode();
        String mobile = memberLoginDTO.getMobile();
        Member dbMember = getMemberByMobile(mobile);
        if (null == dbMember){
            throw new BusinessException(BusinessCode.MEMBER_IS_NOT_EXIST);
        }
        //TODO 校验短信验证码
        if(!"8888".equals(code)){
            throw new BusinessException(BusinessCode.VALIDATE_CODE_ERROR);
        }
        //TODO 生成token
        MemberVO memberVO = BeanUtil.copyProperties(dbMember, MemberVO.class);
        String token = jwtUtil.createToken(dbMember.getId(), mobile);
        memberVO.setToken(token);
        return memberVO;
    }

    /**
     * 根据手机号查询会员
     * @param mobile
     * @return
     */
    public Member getMemberByMobile(String mobile){
        MemberExample example = new MemberExample();
        example.createCriteria().andMobileEqualTo(mobile);
        List<Member> members = memberMapper.selectByExample(example);
        if (members.size() > 0){
            return members.get(0);
        }
        return null;
    }
}
